package com.mobis.web.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class WebFilter extends ZuulFilter{
    @Override
    public String filterType() {
        return "per";  //前置过滤器
    }

    @Override
    public int filterOrder() {
        return 0;  //数字越大优先级越低
    }

    @Override
    public boolean shouldFilter() {
        return true; //为true说明执行该过滤器
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("zuul过滤器");
        //zuul过滤器过滤后，自动清除header，为了避免header丢失，需要准发一下
        RequestContext requestContext=RequestContext.getCurrentContext();
        //获取header
        HttpServletRequest request=requestContext.getRequest();
        String authorization=request.getHeader("Authorization");
        if (null!=authorization){
            requestContext.addZuulRequestHeader("authorization",authorization);
        }


        return null;
    }
}
